package com.adjy.pathbuilders.screens;

import static com.adjy.pathbuilders.commons.Preconditions.checkNotNull;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;

import com.adjy.pathbuilders.R;
import com.adjy.pathbuilders.objects.button.Button;
import com.adjy.pathbuilders.objects.button.PaintButton;
import com.adjy.pathbuilders.objects.button.listeners.BackToMainMenuButtonListener;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.rendering.loading.AnimationFactory;
import com.adjy.pathbuilders.rendering.loading.WeakReferenceBitmap;
import com.adjy.pathbuilders.state.Game;

public class Tutorial implements GameScreen {

	private PaintButton exitButton;
	private List<ScreenPage> pages;
	private ScreenPage currentPage;
	
	public Tutorial(AnimationFactory factory, Game game) {
		checkNotNull(factory, "factory must not be null.");
		checkNotNull(game, "game must not be null.");
		
		Paint rectPaint = rectPaint();
		Paint textPaint = textPaint();
		
		exitButton = new PaintButton(300, 400, 200, 50,
				new BackToMainMenuButtonListener(game),
				game.getContext().getString(R.string.back_to_main_menu),
				rectPaint, textPaint);
		
		pages = createPages(factory, game);
		configurePages(pages);
		
		currentPage = pages.get(0);
	}
	
	private List<ScreenPage> createPages(AnimationFactory factory, Game game) {
		List<Button> empty = new ArrayList<Button>();
	
		ScreenPage page1 = new ScreenPage(
				new WeakReferenceBitmap(factory, "tutorial_bg_1"), factory, exitButton, empty);
		ScreenPage page2 = new ScreenPage(
				new WeakReferenceBitmap(factory, "tutorial_bg_2"), factory, exitButton, empty);
		ScreenPage page3 = new ScreenPage(
				new WeakReferenceBitmap(factory, "tutorial_bg_3"), factory, exitButton, empty);
		
		List<ScreenPage> pages = new ArrayList<ScreenPage>();
		pages.add(page1);
		pages.add(page2);
		pages.add(page3);
		
		return pages;
	}
	
	// FIXME must create a PageMaster to put this code 
	private void configurePages(List<ScreenPage> pages) {
		if (pages.size() > 1) {
			configureFirst(pages);
			
			for (int i = 1; i < pages.size() - 1; i++) {
				pages.get(i).setPrevious(pages.get(i - 1));
				pages.get(i).setNext(pages.get(i + 1));
			}
			
			configureLast(pages);
		}
	}

	private void configureFirst(List<ScreenPage> pages) {
		pages.get(0).setNext(pages.get(1));
	}
	
	private void configureLast(List<ScreenPage> pages) {
		pages.get(pages.size() - 1).setPrevious(pages.get(pages.size() - 2));
	}
	
	private Paint textPaint() {
		Paint textPaint = new Paint();
		textPaint.setColor(Color.BLACK);
		textPaint.setAntiAlias(true);
		textPaint.setTextSize(14);
		return textPaint;
	}

	private Paint rectPaint() {
		Paint rectPaint = new Paint();
		rectPaint.setAntiAlias(true);
		rectPaint.setColor(Color.CYAN);
		return rectPaint;
	}
	
	@Override
	public void load() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void release() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void input(MotionEvent event) {
		// it passes the event to the page
		// then get the next page to be shown and 
		// reset the previous page
		currentPage.input(event);
		ScreenPage last = currentPage; 
		currentPage = currentPage.getNextPage();
		last.reset();
	}

	@Override
	public void render(GameCanvas mcanvas, float timeElapsed) {
		currentPage.render(mcanvas, timeElapsed);
	}

	@Override
	public void step(float timePassed) {
		// TODO Auto-generated method stub
		
	}

	public void reset() {
		// reset the class and the pages
		for (ScreenPage page : pages) {
			page.reset();
		}
		currentPage = pages.get(0);
	}	
}
